<?php
namespace jy_kexie;
defined('BY_JYA') or exit('error');
/*
	课程
*/
class lesson extends \plugin{
	/*
		获取最近课程
	*/
	public function get_lessonlast($body=array()){
		$class=pdo_get('jy_kexie_class',array('id'=>$body['classid']));
		$class['class_lesson']=json_decode($class['class_lesson'],true);
		$weekid=date("w");
		$ymd=date('Y-m-d',TIMESTAMP);
		paixu($class['class_lesson'],'xinqi','asc');
		foreach($class['class_lesson'] as $item){
			if($item['xinqi']==0 || $item['xinqi']>=$weekid){
					$item['shijian']=strtotime($ymd.' '.$item['time']['start']);
					if($item['shijian']>TIMESTAMP){
						$lessontitle=pdo_getcolumn('jy_kexie_lesson',array('id'=>$item['lessonid']),'title');
						$lessontime=$this->week_days[$item['xinqi']].'~'.$item['time']['start'];
						break;
					}
			}
		}
		if($lessontitle){
			$lessonlast='《'.$lessontitle.'》'.$lessontime;
		}else{
			$lessonlast='本周没有任何课程';
		}
		return $lessonlast;
	}
	public function get_days($classid,$days=7){
		$class=pdo_get('jy_kexie_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$classid));
		//日期
		$data=array();
		$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
		for($i=0;$i<$days;$i++){
			$data[]=array(
				'ymd'=>date("Y-m-d",strtotime("+".$i." day")),
				'riqi'=>date("m-d",strtotime("+".$i." day")),
				'xinq'=>"星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))))],
			);
		}
		return $data;
	}
	public function get_uptime($ontime,$isarray=false){
		$hours=date('H',$ontime);
		$weekid=date("w",$ontime);
		$week=$this->week_days[$weekid];
		$ontimeinfo='【'.$week.'】'.date('Y-m-d',$ontime);
		if($hours>12){
				$ontimeinfo .=' [下午]';
		}else{
				$ontimeinfo .=' [上午]';
		}
		if($isarray){
			$res=array(
				'hours'=>$hours,
				'weekid'=>$weekid,
				'days'=>date('Y-m-d',$ontime),
				'ontimeinfo'=>$ontimeinfo
			);
			return $res;
		}else{
			return $ontimeinfo;
		}
	}
	public function get_endtime($ontime,$on_num,$workerid,$isarray=array()){
		$worker=pdo_get('jy_kexie_worker',array('id'=>$workerid,'uniacid'=>$_SESSION['uniacid']));
		$worker['on_days']=str_array($worker['on_days']);
		for($i=0;$i<$on_num;$i++){
			
		}
		$hours=date('H',$ontime);
		$weekid=date("w",$ontime);
		$week=$this->week_days[$weekid];
		$ontimeinfo='【'.$week.'】'.date('Y-m-d',$ontime);
		if($hours>12){
			$ontimeinfo .=' [下午]';
		}else{
			$ontimeinfo .=' [上午]';
		}
		if($isarray){
			$res=array(
				'hours'=>$hours,
				'weekid'=>$weekid,
				'days'=>date('Y-m-d',$ontime),
				'ontimeinfo'=>$ontimeinfo
			);
			return $res;
		}else{
			return $ontimeinfo;
		}
	}
	public function get_months($workerid=0,$selectyear=0,$isdingtype=0,$iteminfo=array()){
		$basic=$this->plugin_setting('basic');
		$basic['on_months']=str_array($basic['on_months']);
		$year = date('Y');
		$mingyear = date('Y')+1;
		$month = date('m');
		$list = array();
		foreach($basic['on_months'] as $bv){
			if($month<=$bv){
				$list[$bv] = array(
					'title'=>$year.'年'.$bv.'月',
					'year'=>$year,
					'month'=>$bv,
					'id'=>$bv
				);
			}else{
				$list[$bv] = array(
					'title'=>$mingyear.'年'.$bv.'月',
					'year'=>$mingyear,
					'month'=>$bv,
					'id'=>$bv
				);
			}
		}
		if($iteminfo['isstore']){
			$on_days=';'.$iteminfo['week_days'].';';
		}else{
			$on_days=pdo_getcolumn('jy_kexie_worker',array('id'=>$workerid),'on_days');
		}
		foreach($list as &$lv){
			$day = $this->getcalendar($lv['month'],$lv['year']);
			if(!$day){
				exi('请先安装Calendar扩展，获取日历信息！','error');
			}
			$lv['daynum']=$day;
			$days = array();
			$i = 1;
			while ($i <= $day){
					if ($i < 10) {
						$i = '0' . $i;
					}
					$daydata=$lv['year'].'-'.$lv['month'].'-'.$i;
					$weekid=date("w",strtotime($daydata));
					//print_r($on_days);exit;
					if(strpos($on_days,';'.$weekid.';') !== false){
						$isadd=pdo_get('jy_kexie_lesson_pai',array('onday'=>$daydata,'ontype'=>0,'workerid'=>$workerid,'uniacid'=>$_SESSION['uniacid']),'id');
						if($isadd['id']&& $isadd['id']!=$iteminfo['id']){
							$isding=1;
						}else{
							$isding=0;
						}
						if($isdingtype==$isding){
							$xingqi=$this->week_days[$weekid];
							$days[$daydata]=array(
								'id'=>$daydata,
								'day'=>$i,
								'title'=>$daydata.'('.$xingqi.')',
								'weekid'=>$weekid,
								'ontype'=>$ontype,
								'isding'=>$isding,
								'noshow'=>$noshow?:false,
							);
						}
					}
					++$i;
			}
			$lv['list']=$days;
		}
		return $list;
	}
	public function getcalendar($month='',$year='')
	{
		$day = date("t",strtotime($year.'-'.$month));
		return $day;
	}
	public function checkpaike($store)
	{
		$paitime=pdo_get('jy_kexie_lesson_paitime',array('year'=>date('Y'),'type'=>1,'storeid'=>$store['id'],'uniacid'=>$_SESSION['uniacid']));
		if($paitime['id']){
			if($paitime['starttime']<time() && $paitime['endtime']>time()){
				return true;
			}
		}
		$paitime=pdo_get('jy_kexie_lesson_paitime',array('year'=>date('Y'),'storetype'=>$store['type'],'uniacid'=>$_SESSION['uniacid']));
		$storetypetitle=$this->storetype[$store['type']];
		if($paitime['id']){
			if($paitime['starttime']<time() && $paitime['endtime']>time()){
				return true;
			}
			$paitime['starttime']=date('Y-m-d',$paitime['starttime']);
			$paitime['endtime']=date('Y-m-d',$paitime['endtime']);
			exi('您校区属于<'.$storetypetitle.'>,选课的时间段:'.$paitime['starttime'].'~'.$paitime['endtime'],'error','goback');
		}else{
			exi('您校区属于<'.$storetypetitle.'>,选课的时间段还未开启！','error','goback');
		}
		return true;
	}
	public function add_lesson_log($params,$uid=''){
		$isadd=$this->pdo_get('lesson_log',array('lesson_id'=>intval($params['lesson_id']),'mid'=>$params['mid']?:$uid,'uniacid'=>$_SESSION['uniacid']));
		if($isadd['id']){
			exi('您已报名该课程！','error','/mylesson_log/');
		}
		$uid=$uid?$uid:$_SESSION['uid'];
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'logsn'=>get_ordersn($this->tablename('lesson_log'),'logsn'),
			'mid'=>$params['mid']?:$uid,
			'storeid'=>intval($params['storeid']),
			'lesson_id'=>intval($params['lesson_id']),
			'day'=>$params['day'],
			'worker_id'=>intval($params['worker_id']),
			'remark'=>$params['remark'],
			'name'=>$params['name'],
			'mobile'=>$params['mobile'],
			'ontime'=>TIMESTAMP,
		);
		$this->pdo_insert('lesson_log',$order);
		$log_id=$this->pdo_insertid();
		return $log_id;
	}
	public function get_onday($lessonid){
			$start=pdo_get('jy_kexie_lesson_pai',array('ontime >'=>time(),'lessonid'=>$lessonid,'uniacid'=>$_SESSION['uniacid']),'','ontime asc');
			if(!$start['onday']){
				$start=pdo_get('jy_kexie_lesson_pai',array('lessonid'=>$lessonid,'uniacid'=>$_SESSION['uniacid']),'','ontime asc');
				$onday=$start['onday']?$start['onday'].'[已结束]':'未排课';
			}else{
				$onday=$start['onday'];
			}
			return $onday;
	}
}